// run: $exec < input
// opt: 0
// flag: -g
#include <iostream>

int const circle = 2058;
struct date { int y, m, d; };

std::istream & operator>>(std::istream & is, date & a)
{
	is >> a.y >> a.m >> a.d;
	return is;
}

std::ostream & operator<<(std::ostream & os, date const & a)
{
	os << a.y << ' ' << a.m << ' ' << a.d << '\n';
	return os;
}

date regular(date const & a)
{
	auto ret = a;
	if (ret.m >= 13) { ret.m -= 12; ret.y++; }
	return ret;
}

date in;
int count[7];
int n;

int calc(int year_part, int m, int d)
{
	int ret = year_part;
	ret += 26 * (m + 1) / 10;
	ret += d - 1;
	ret = ((ret % 7) + 7) % 7;
	return ret;
}

void init()
{
	for (int i = 0; i < 7; i++)
		for (int j = 3; j <= 14; j++) {
			count[i] += calc(i, j, 1) == 1;
			count[i] += calc(i, j, 11) == 1;
			count[i] += calc(i, j, 21) == 1;
		}
}

int calc_year_part(int y)
{
	int ty = y % 100, tc = y / 100;
	int ret = ty + int(ty / 4) + int(tc / 4) - 2 * tc;
	ret = ((ret % 7) + 7) % 7;
	return ret;
}

date nth_from_now(date const & a, int n)
{
	auto tmp = a;
	if (tmp.m <= 2) { tmp.m += 12; tmp.y--; }
	if (tmp.d > 21) {
		tmp.d = 1; tmp.m++;
		if (tmp.m == 15) { tmp.m = 3; tmp.y++; }
	}
	auto t = calc_year_part(tmp.y);
//	std::cout << "::  " << tmp.y << ' ' << t << ' ' <<calc(t, 1, 11) << '\n';
	for (int i = tmp.m; i <= 14; i++) {
		if (tmp.d == 1) {
			if (calc(t, i, 1) == 1) {
				n--;
				if (!n) return tmp;
				tmp.d = 11;
			}
		}
		if (tmp.d <= 11) {
			tmp.d = 11;
			if (calc(t, i, 11) == 1) {
				n--;
				if (!n) return tmp;
				tmp.d = 21;
			}
		}
		if (tmp.d <= 21) {
			tmp.d = 21;
			if (calc(t, i, 21) == 1) {
				n--;
				if (!n) return tmp;
			}
		}
		tmp.d = 1;
		tmp.m++;
		if (tmp.m == 15) { tmp.m = 3; tmp.y++; }
	}
//	std::cout << "::" << tmp << n << ' ' << count[calc_year_part(tmp.y)]<< '\n';
	for (int i = 0; i < 500; i++) {
		auto t = calc_year_part(tmp.y);
		if (count[t] >= n) {
			return nth_from_now(tmp, n);
		} else {
			n -= count[t];
			tmp.y++;
		}
	}
	return tmp;
}

int calc_lucky_to_year(date const & a, int year)
{
	auto tmp = a;
	if (tmp.m <= 2) { tmp.m += 12; tmp.y--; }
	if (tmp.d > 21) {
		tmp.d = 1; tmp.m++;
		if (tmp.m == 15) { tmp.m = 3; tmp.y++; }
	}
	auto t = calc_year_part(tmp.y);
	int ret = 0;
	for (int i = tmp.m; i <= 14; i++) {
		if (tmp.d == 1) {
			if (calc(t, i, 1) == 1) {
				ret++;
				tmp.d = 11;
			}
		}
		if (tmp.d <= 11) {
			tmp.d = 11;
			if (calc(t, i, 11) == 1) {
				ret++;
				tmp.d = 21;
			}
		}
		if (tmp.d <= 21) {
			tmp.d = 21;
			if (calc(t, i, 21) == 1)
				ret++;
		}
		tmp.d = 1;
		tmp.m++;
		if (tmp.m == 15) { tmp.m = 3; tmp.y++; }
	}
	for (; tmp.y < year; tmp.y++)
		ret += count[calc_year_part(tmp.y)];
	return ret - 1;
}

int main()
{
	std::ios::sync_with_stdio(false);
	int T; std::cin >> T;
	init();
	while (T--) {
		std::cin >> in >> n;
		if (n <= circle)
			std::cout << regular(nth_from_now(in, n));
		else {
			int year = (in.y / 400) * 400 + 400;
			int tmp = calc_lucky_to_year(in, year);
			n -= tmp;
			year += (n / circle) * 400;
			n %= circle;
			std::cout << regular(nth_from_now({year, 1, 1}, n));
		}
	}
}

